#! /bin/bash

if [[ $1 == "" ]];then
  echo -e "\e[32mUsage:\e[39m"
  echo "  $0 BUILD"
  echo " "
  exit 1
fi
build=$1

set -euo pipefail

namespace="nsgate"
service="nsgate"

# load configuration
root_directory="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/../.."
. ${root_directory}/deploy/config

# generate manifest
manifest_directory="${root_directory}/deploy/kubernetes/manifests"
templates_directory="${root_directory}/deploy/kubernetes/templates"
replication_controller_file="${manifest_directory}/${service}-rc.yml"

mkdir -p ${manifest_directory}

cp ${templates_directory}/${service}/rc.yml ${replication_controller_file}
sed -ie "s/__MAJOR__/${nsGateContainer_major}/g" ${replication_controller_file}
sed -ie "s/__MINOR__/${nsGateContainer_minor}/g" ${replication_controller_file}
sed -ie "s/__BUILD__/${build}/g" ${replication_controller_file}
sed -ie "s|__DOCKERIMAGE__|${nsGateContainer}.${build}|g" ${replication_controller_file}
sed -ie "s/__NAMESPACE__/${namespace}/g" ${replication_controller_file}

# deploy on kubernetes
kubectl="${root_directory}/deploy/clis/kubectl"
manifests_directory_in_container="deploy/kubernetes/manifests"
${kubectl} delete -f ${manifests_directory_in_container}/nsgate-rc.yml
${kubectl} create -f ${manifests_directory_in_container}/nsgate-rc.yml

# get the namespace informations
describe="${root_directory}/deploy/kubernetes/describe"
echo " "
echo " "
${describe} ${namespace}
echo " "
echo " "
echo " Wait a moment and run <describe ${namespace}> to see the PODS generated on your cluster"
